<?php
namespace Views\Widgets;
use Models\MenuItem;
use Models\User;
use Path;

/**
 * @author Evgeny Baranov <evgeny@baranov.eu>
 */

class Menu extends Base {

    /**
     * @var string
     */
    protected $tmpl = 'top.phtml';

    /**
     * @var string
     */
    protected $block = 'Menu';

    /**
     * @var null
     */
    protected static $current = null;


    protected $menu;
    /**
     * @var array
     */
    protected static $items = array(
        'name' => 'root',
        'path' => '',
        'menu' => array(
            array(
                'name' => 'Articles',
                'path' => 'articles',
                'controller' => 'article',
                'role' => User::ROLE_GUEST
            ),
            array(
                'name' => 'Children training',
                'path' => 'children',
                'role' => User::ROLE_GUEST,
                'controller' => 'article',
                'action' => 'view',
                'param' => array(5)
            ),
            array(
                'name' => 'Contacts',
                'path' => 'contacts',
                'role' => User::ROLE_GUEST,
                'controller' => 'article',
                'action' => 'view',
                'param' => array(2)
            ), array(
                'name' => 'Admin',
                'role' => User::ROLE_WRITER,
                'path' => 'admin',
                'ns' => 'Admin',
                'id' => 'dd-admin-menu',
                'menu' => array(
                    array(
                        'name' => 'Add article',
                        'path' => 'add_article',
                        'controller' => 'Article',
                        'action' => 'create',
                        'role' => User::ROLE_WRITER
                    ),
                    array(
                        'name' => 'Users',
                        'path' => 'user',
                        'controller' => 'User',
                        'action' => 'index',
                        'role' => User::ROLE_ADMIN
                    ),
                    array(
                        'name' => 'Translations',
                        'controller' => 'Translation',
                        'path' => 'translation',
                        'role' => User::ROLE_ADMIN
                    )
                )
            )
        )
    );

    /**
     * @param $items
     * @return $this
     */
    protected function buildMenu($items) {
        $this->menu = $items;
        foreach ($this->menu as &$data) {
            $data = new MenuItem($data);
        }

        $this->setData('menu', $this->menu);

        return $this;
    }

    public function __construct() {
        $this->setData('menu', self::$items);
        parent::__construct();
    }

    /**
     * @param \App $app
     */
    public static function route(\App &$app) {
        $route = Path::getInstance();
        self::$items = new MenuItem(self::$items);
        self::$current = self::$items->route();

        $c = self::$current->getController();

        if ($c) {
            $app->setController($c);
            $a = self::$current->getAction();
            if ($a) {
                $app->setAction($a);
            }
            $route->append(self::$current->getParam());
        }
    }
}